#include <myheader.h>
//定义互斥锁
static pthread_mutex_t mutex;
//共享资源
static int shared_var = 0;


void* thread_func(void* arg){
    for(int i = 0; i < 100000; i++){
        //mutual exclusion
        pthread_mutex_lock(&mutex);
        shared_var++;
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
/*Usage:  */
int main(int argc, char *argv[])
{
    //主线程


    pthread_t thread1,thread2;
    //初始化互斥锁
    pthread_mutex_init(&mutex,NULL);
    
    //create two thrreads
    pthread_create(&thread1,NULL,thread_func,NULL);
    pthread_create(&thread2,NULL,thread_func,NULL);

    //wait thread end
    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);

    //recycle 
    pthread_mutex_destroy(&mutex);
    printf("%d\n",shared_var);



    return 0;
}

